﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Management;
using System.Net;

namespace MP.Core.Library
{
    public class WindowsUtil
    {
        
        //public static string GetCurrentUser(string domainName,Hashtable excludeUsers)
        //{
        //    Process[] procs = Process.GetProcesses();
        //    for (int i = 0; i < procs.Length; i++)
        //    {
        //        String User = "";
        //        String Domain = "";
              
        //        GetProcessInfoByPID(procs[i].Id, out User, out Domain);
        //        if (
        //            Domain.ToUpper() == domainName.ToUpper()
        //            &&User.ToUpper() != "SYSTEM"
        //            && User.ToUpper() != "LOCAL SERVICE"
        //            && User.ToUpper() != "NETWORK SERVICE"
        //            && User.ToUpper() != "ADMINISTRATOR"
        //            && !excludeUsers.ContainsValue(User.ToUpper())
        //            )
        //        {
        //            return User;
        //        }
        //    }
        //    return String.Empty;
        //}

        
        public static string GetProcessInfoByPID(int PID, out string User, out string Domain)
        {
           
            //  log.Start("GetProcessInfoByPID");
            User = String.Empty;
            Domain = String.Empty;
            String OwnerSID = String.Empty;
            string processname = String.Empty;
            try
            {
                ObjectQuery sq = new ObjectQuery
                    ("Select * from Win32_Process Where ProcessID = '" + PID + "'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq);
                if (searcher.Get().Count == 0)
                    return OwnerSID;
                foreach (ManagementObject oReturn in searcher.Get())
                {
                    string[] o = new String[2];
                    //Invoke the method and populate the o var with the user name and domain
                    oReturn.InvokeMethod("GetOwner", (object[])o);

                    //int pid = (int)oReturn["ProcessID"];
                    processname = (string)oReturn["Name"];
                    //dr[2] = oReturn["Description"];
                    User = o[0];
                    if (User == null)
                        User = String.Empty;
                    Domain = o[1];
                    if (Domain == null)
                        Domain = String.Empty;
                    string[] sid = new String[1];
                    oReturn.InvokeMethod("GetOwnerSid", (object[])sid);
                    OwnerSID = sid[0];
                    //  log.End("GetProcessInfoByPID");
                    return OwnerSID;
                }
            }
            catch
            {
                //  log.End("GetProcessInfoByPID");
                return OwnerSID;
            }
            // log.End("GetProcessInfoByPID");
            return OwnerSID;
        }

        public static string GetIPAddress()
        {
            IPHostEntry ipEntry = Dns.GetHostByName(Dns.GetHostName());
            IPAddress[] addr = ipEntry.AddressList;
            String ip = addr.Length > 0 ? addr[0].ToString() : "";
            return ip;
        }

        public string GetMACAddress()
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            string MACAddress = String.Empty;
            foreach (ManagementObject mo in moc)
            {
                if (MACAddress == String.Empty) // only return MAC Address from first card
                {
                    if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
                }
                mo.Dispose();
            }

            MACAddress = MACAddress.Replace(":", "");
            return MACAddress;
        } 
    }
}
